//forum_question_answer_collect
//问题回答收藏
exports.user_collect = function (req, res) {

    var bodyParams = req.routeInfo.bodyParams;
    //最终结果
    var resultData = {};

    var collectData = 0;

    var collectCountData = 0;

    //任务流创建
    async.waterfall([
            //1.查询收藏记录表
            function (cb) {
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.userSearchCollect + "/uc_type/" + bodyParams.uc_type + "/uc_fid/" + bodyParams.uc_fid + "/uc_uid/" + bodyParams.uc_uid,
                        method: 'POST',
                        data: {
                            fields: ["uc_id", "is_delete"].join(",")
                        }
                    },
                    function (err, data) {
                        // //console.log(data);
                        //判断是否有错误信息
                        if (err) {
                            return RES.response(res, false, data);
                        }
                        // resultData.collectData = data.data;

                        collectData = data.data;
                        //console.log("收藏记录：" + collectData.length);
                        cb();
                    }
                );
            },
            //1.判断是否有收藏记录
            function (cb) {
                if (collectData.length > 0) {
                    if (collectData[0].is_delete <= 0) {
                        R.SEND_HTTP(
                            req,
                            {
                                url: CORE_APIConfig.coreAPI.updateCollect,
                                method: 'POST',
                                data: {
                                    uc_id: collectData[0].uc_id,
                                    is_delete: 1
                                }
                            },
                            function (err, data) {
                                // //console.log(data);
                                //判断是否有错误信息
                                if (err) {
                                    return RES.response(res, false, data);
                                }
                                resultData.cancleCollect = "已取消收藏";
                                cb();
                            }
                        );
                    } else {
                        R.SEND_HTTP(
                            req,
                            {
                                url: CORE_APIConfig.coreAPI.updateCollect,
                                method: 'POST',
                                data: {
                                    uc_id: collectData[0].uc_id,
                                    is_delete: 0
                                }
                            },
                            function (err, data) {
                                // //console.log(data);
                                //判断是否有错误信息
                                if (err) {
                                    return RES.response(res, false, data);
                                }
                                resultData.reCollect = "收藏成功";
                                cb();
                            }
                        );
                    }
                    //添加收藏
                } else {
                    R.SEND_HTTP(
                        req,
                        {
                            url: CORE_APIConfig.coreAPI.addUserCollect,
                            method: 'POST',
                            data: {
                                uc_fid: bodyParams.uc_fid,
                                uc_uid: bodyParams.uc_uid,
                                uc_type: bodyParams.uc_type
                            }
                        },
                        function (err, data) {
                            // //console.log(data);
                            //判断是否有错误信息
                            if (err) {
                                return RES.response(res, false, data);
                            }
                            // resultData.collectData = data.data;

                            var addCollectData = data.data;

                            resultData.addCollectData = "收藏成功";
                            //console.log(addCollectData);

                            cb();
                        }
                    );
                }
            },

            //查询数量
            function (cb) {
                //文章回答收藏数
                if (bodyParams.uc_type == 4) {
                    R.SEND_HTTP(
                        req,
                        {
                            url: CORE_APIConfig.coreAPI.searchContentAnswer + "/fca_id/" + bodyParams.uc_fid,
                            method: 'POST',
                            data: {
                                fields: ["fca_id", "fca_collect_count"].join(",")
                            }
                        },
                        function (err, data) {
                            //console.log(data);
                            //判断是否有错误信息
                            if (err) {
                                return RES.response(res, false, data);
                            }

                            collectCountData = data.data;
                            cb();
                        }
                    );
                }
                else if (bodyParams.uc_type == 2) {
                    R.SEND_HTTP(
                        req,
                        {
                            url: CORE_APIConfig.coreAPI.searchForumContent + "/fc_id/" + bodyParams.uc_fid,
                            method: 'POST',
                            data: {
                                fields: ["fc_id", "fc_collect_count"].join(",")
                            }
                        },
                        function (err, data) {
                            //console.log(data);
                            //判断是否有错误信息
                            if (err) {
                                return RES.response(res, false, data);
                            }

                            collectCountData = data.data;
                            cb();
                        }
                    );
                }
                else if (bodyParams.uc_type == 3) {
                    R.SEND_HTTP(
                        req,
                        {
                            url: CORE_APIConfig.coreAPI.searchForumActivity + "/fa_id/" + bodyParams.uc_fid,
                            method: 'POST',
                            data: {
                                fields: ["fa_id", "fa_collect_count"].join(",")
                            }
                        },
                        function (err, data) {
                            //console.log(data);
                            //判断是否有错误信息
                            if (err) {
                                return RES.response(res, false, data);
                            }

                            collectCountData = data.data;
                            cb();
                        }
                    );
                }else{
                    cb()
                }
            },

            //1.更新收藏数
            function (cb) {
                if (collectData.length !== 0) {
                    if (bodyParams.uc_type == 4) {
                        if (collectData[0].is_delete <= 0) {
                            R.SEND_HTTP(
                                req,
                                {
                                    url: CORE_APIConfig.coreAPI.updateContentAnswer,
                                    method: 'POST',
                                    data: {
                                        fca_id: bodyParams.uc_fid,
                                        fca_collect_count: parseInt(collectCountData[0].fca_collect_count) - 1
                                    }
                                },
                                function (err, data) {
                                    // //console.log(data);
                                    //判断是否有错误信息
                                    if (err) {
                                        return RES.response(res, false, data);
                                    }
                                    // resultData.collectData = data.data;

                                    var updateData = data.data;

                                    //console.log(updateData);

                                    cb();
                                }
                            );
                        } else {
                            R.SEND_HTTP(
                                req,
                                {
                                    url: CORE_APIConfig.coreAPI.updateContentAnswer,
                                    method: 'POST',
                                    data: {
                                        fca_id: bodyParams.uc_fid,
                                        fca_collect_count: parseInt(collectCountData[0].fca_collect_count) + 1
                                    }
                                },
                                function (err, data) {
                                    // //console.log(data);
                                    //判断是否有错误信息
                                    if (err) {
                                        return RES.response(res, false, data);
                                    }
                                    // resultData.collectData = data.data;

                                    var updateData = data.data;

                                    //console.log(updateData);

                                    cb();
                                }
                            );
                        }
                    }
                    else if (bodyParams.uc_type == 2) {
                        if (collectData[0].is_delete <= 0) {
                            R.SEND_HTTP(
                                req,
                                {
                                    url: CORE_APIConfig.coreAPI.updateForumContent,
                                    method: 'POST',
                                    data: {
                                        fc_id: bodyParams.uc_fid,
                                        fc_collect_count: parseInt(collectCountData[0].fc_collect_count) - 1
                                    }
                                },
                                function (err, data) {
                                    // //console.log(data);
                                    //判断是否有错误信息
                                    if (err) {
                                        return RES.response(res, false, data);
                                    }
                                    // resultData.collectData = data.data;

                                    var updateData = data.data;

                                    //console.log(updateData);

                                    cb();
                                }
                            );
                        } else {
                            R.SEND_HTTP(
                                req,
                                {
                                    url: CORE_APIConfig.coreAPI.updateForumContent,
                                    method: 'POST',
                                    data: {
                                        fc_id: bodyParams.uc_fid,
                                        fc_collect_count: parseInt(collectCountData[0].fc_collect_count) + 1
                                    }
                                },
                                function (err, data) {
                                    // //console.log(data);
                                    //判断是否有错误信息
                                    if (err) {
                                        return RES.response(res, false, data);
                                    }
                                    // resultData.collectData = data.data;

                                    var updateData = data.data;

                                    //console.log(updateData);

                                    cb();
                                }
                            );
                        }
                    }
                    else if (bodyParams.uc_type == 3) {
                        if (collectData[0].is_delete <= 0) {
                            R.SEND_HTTP(
                                req,
                                {
                                    url: CORE_APIConfig.coreAPI.updateActivity,
                                    method: 'POST',
                                    data: {
                                        fa_id: bodyParams.uc_fid,
                                        fa_collect_count: parseInt(collectCountData[0].fa_collect_count) - 1
                                    }
                                },
                                function (err, data) {
                                    // //console.log(data);
                                    //判断是否有错误信息
                                    if (err) {
                                        return RES.response(res, false, data);
                                    }
                                    // resultData.collectData = data.data;

                                    var updateData = data.data;

                                    //console.log(updateData);

                                    cb();
                                }
                            );
                        } else {
                            R.SEND_HTTP(
                                req,
                                {
                                    url: CORE_APIConfig.coreAPI.updateActivity,
                                    method: 'POST',
                                    data: {
                                        fa_id: bodyParams.uc_fid,
                                        fa_collect_count: parseInt(collectCountData[0].fa_collect_count) + 1
                                    }
                                },
                                function (err, data) {
                                    // //console.log(data);
                                    //判断是否有错误信息
                                    if (err) {
                                        return RES.response(res, false, data);
                                    }
                                    // resultData.collectData = data.data;

                                    var updateData = data.data;

                                    //console.log(updateData);

                                    cb();
                                }
                            );
                        }
                    }else{
                        cb();
                    }
                } else {
                    if (bodyParams.uc_type == 4) {
                        R.SEND_HTTP(
                            req,
                            {
                                url: CORE_APIConfig.coreAPI.updateContentAnswer,
                                method: 'POST',
                                data: {
                                    fca_id: bodyParams.uc_fid,
                                    fca_collect_count: parseInt(collectCountData[0].fca_collect_count) + 1
                                }
                            },
                            function (err, data) {
                                // //console.log(data);
                                //判断是否有错误信息
                                if (err) {
                                    return RES.response(res, false, data);
                                }
                                // resultData.collectData = data.data;

                                var updateData = data.data;

                                //console.log(updateData);

                                cb();
                            }
                        );
                    }
                    else if (bodyParams.uc_type == 2) {
                        R.SEND_HTTP(
                            req,
                            {
                                url: CORE_APIConfig.coreAPI.updateForumContent,
                                method: 'POST',
                                data: {
                                    fc_id: bodyParams.uc_fid,
                                    fc_collect_count: parseInt(collectCountData[0].fc_collect_count) + 1
                                }
                            },
                            function (err, data) {
                                // //console.log(data);
                                //判断是否有错误信息
                                if (err) {
                                    return RES.response(res, false, data);
                                }
                                // resultData.collectData = data.data;

                                var updateData = data.data;

                                //console.log(updateData);

                                cb();
                            }
                        );
                    }
                    else if (bodyParams.uc_type == 3) {
                        R.SEND_HTTP(
                            req,
                            {
                                url: CORE_APIConfig.coreAPI.updateActivity,
                                method: 'POST',
                                data: {
                                    fa_id: bodyParams.uc_fid,
                                    fa_collect_count: parseInt(collectCountData[0].fa_collect_count) + 1
                                }
                            },
                            function (err, data) {
                                // //console.log(data);
                                //判断是否有错误信息
                                if (err) {
                                    return RES.response(res, false, data);
                                }
                                // resultData.collectData = data.data;

                                var updateData = data.data;

                                //console.log(updateData);

                                cb();
                            }
                        );
                    }else{
                        cb()
                    }
                }
            },
        ], function (err, result) {

            //返回最终拼接结果
            RES.response(res, true, resultData);

        }
    );

};